var PrefabManager = cc.Class({
  extends: cc.Component,

  properties: () => ({
    // 预制资源
    monster1: {
      default: null,
      type: cc.Prefab
    },
    monster2: {
      default: null,
      type: cc.Prefab
    },
    monster3: {
      default: null,
      type: cc.Prefab
    },
    monster4: {
      default: null,
      type: cc.Prefab
    },
    monster5: {
      default: null,
      type: cc.Prefab
    },
    monster6: {
      default: null,
      type: cc.Prefab
    },
    // 怪物存放的节点
    mstLay: {
      default: null,
      type: cc.Node
    },
  }),
  onLoad: function() {
    // 怪物移动速度
    this.mstMoveSpeed = 0,
    this.mstArr = [];
    // this._armatureDisPlay = this.getComponent(dragonBones.ArmatureDisplay);
    // this._armature = this._armatureDisPlay.armature();
  },
  // 移动怪物
  moveMst: function(distance) {
    if (this.mstArr) {
      for (var i = 0; i < this.mstArr.length; ++i) {
        this.mstArr[i].x -= this.mstMoveSpeed;
        if (this.mstArr[i].x <= -1200) {
          this.mstLay.removeChild(this.mstArr[i]);
          this.mstArr.splice(this.mstArr[i], 1);
        }
      }
    }
    // cc.log(this.mstMoveSpeed,"输出速度");
  },
  // 停止移动
  stopMove: function(other) {
    var mstOne = other;
    // 如果删除会出现前面的怪物停留的bug
    // this.mstArr.splice(mstOne,1);
    // 缓动动作 击退效果
    if (mstOne.y >= 70) {
      var action = cc.moveBy(0.1, cc.v2(200, 0)).easing(cc.easeCircleActionIn());
    } else {
      var action = cc.moveBy(0.1, cc.v2(200, 0)).easing(cc.easeCircleActionIn());
    }
    // 执行动作
    mstOne.runAction(action);
    var delMst = function(dt) {
      this.mstLay.removeChild(mstOne);
    };
    this.scheduleOnce(delMst, 0.2);
  },
  // 创建怪物
  creatrMst: function(xx) {
    if (xx == 101) {
      var mst1 = cc.instantiate(this.monster1);
      this.mstLay.addChild(mst1);
      mst1.x = 0;
      mst1.y = Math.floor(Math.random() * 10) - 90;
      this.mstArr.push(mst1);
      mst1.getComponent(dragonBones.ArmatureDisplay).playAnimation('jump', 1);
      this.scheduleOnce(function() {
        mst1.getComponent(dragonBones.ArmatureDisplay).playAnimation('run', 0);
      }, 0.3);

    }else if (xx == 102) {
      var mst2 = cc.instantiate(this.monster2);
      this.mstLay.addChild(mst2);
      mst2.x = 0;
      mst2.y = Math.floor(Math.random() * 10) - 90;
      this.mstArr.push(mst2);
      mst2.getComponent(dragonBones.ArmatureDisplay).playAnimation('jump', 1);

      this.scheduleOnce(function() {
        mst2.getComponent(dragonBones.ArmatureDisplay).playAnimation('run', 0);
      }, 0.3);
    }else if (xx == 103) {
      var mst3 = cc.instantiate(this.monster3);
      this.mstLay.addChild(mst3);
      mst3.x = 0;
      mst3.y = Math.floor(Math.random() * 10) - 90;
      this.mstArr.push(mst3);
      mst3.getComponent(dragonBones.ArmatureDisplay).playAnimation('jump', 1);

      this.scheduleOnce(function() {
        mst3.getComponent(dragonBones.ArmatureDisplay).playAnimation('run', 0);
      }, 0.3);
    }else if (xx == 104) {
      var mst4 = cc.instantiate(this.monster4);
      this.mstLay.addChild(mst4);
      mst4.x = 0;
      mst4.y = Math.floor(Math.random() * 10) - 90;
      this.mstArr.push(mst4);
      mst4.getComponent(dragonBones.ArmatureDisplay).playAnimation('jump', 1);

      this.scheduleOnce(function() {
        mst4.getComponent(dragonBones.ArmatureDisplay).playAnimation('run', 0);
      }, 0.3);
    }else if (xx == 105) {
      var mst5 = cc.instantiate(this.monster5);
      this.mstLay.addChild(mst5);
      mst5.x = 0;
      mst5.y = Math.floor(Math.random() * 10) - 90;
      this.mstArr.push(mst5);
      mst5.getComponent(dragonBones.ArmatureDisplay).playAnimation('jump', 1);

      this.scheduleOnce(function() {
        mst5.getComponent(dragonBones.ArmatureDisplay).playAnimation('run', 0);
      }, 0.3);
    }else if (xx == 106) {
      var mst6 = cc.instantiate(this.monster6);
      this.mstLay.addChild(mst6);
      mst6.x = 0;
      mst6.y = Math.floor(Math.random() * 10) - 90;
      this.mstArr.push(mst6);
      mst6.getComponent(dragonBones.ArmatureDisplay).playAnimation('jump', 1);

      this.scheduleOnce(function() {
        mst6.getComponent(dragonBones.ArmatureDisplay).playAnimation('run', 0);
      }, 0.3);
    }
  },
  // 停止创建
  stopAddMst: function() {
    this.unschedule(this.creatrMst);
  },
  // 删除怪物
  delMonster: function(node) {
    var mst = node;
    this.mstLay.removeChild(mst);
    this.mstArr.splice(mst, 1);
  },
  // 停止游戏后删除所有怪物
  delAllMst: function() {
    this.mstLay.removeAllChildren();
    // 以下做法会有bug 停止游戏后还有残留 因为update时间太快导致
    // if(this.mstArr){
    //     for(var i = 0; i < this.mstArr.length; ++i){
    //         this.mstLay.removeChild(this.mstArr[i]);
    //         this.mstArr.splice(this.mstArr[i],1);
    //     }
    // }
  }
});
module.exports = PrefabManager;